#include <iostream>
#include "../../common/log.hpp"
#include "../../common/uuid.hpp"
#include "../../common/message.hpp"

using namespace ns_jsonrpc;
int main()
{
    // -------------------- 测试日志以及UUID生成器 ------------------------------
    LOG_DEBUG("%s, %d\n", "hello world", 90);
    LOG_ERROR("%d\n", sizeof(size_t));
    LOG_INFO("uuid:%s\n", UUID::uuid().c_str());

    // -------------------- 消息派生类测试-------------------------------------
    // 1.rpc请求
    RpcRequest::RpcRequestPtr rpcReq = MessageFactory::create<RpcRequest>();
    rpcReq->setId("123");
    rpcReq->setMType(MType::REQ_RPC);
    rpcReq->setMethod("add");
    Json::Value params1;
    params1["num1"] = 1;
    params1["num2"] = 2;
    rpcReq->setParams(params1);
    BaseMessage::BaseMessagePtr msg = rpcReq; // 基类指针指向父类对象
    if (!msg->check())
    {
        LOG_ERROR("rpc request parameters is error\n");
        return 0;
    }
    std::string msgStr = msg->serialize();
    std::cout << "msgStr: " << msgStr << std::endl;

    // 2.rpc响应
    RpcResponse::RpcResponsePtr rpcRsp = MessageFactory::create<RpcResponse>();
    rpcRsp->setId(msg->id());
    rpcRsp->setMType(MType::RSP_RPC);
    Json::Value params2 = rpcReq->params();
    int num1 = params2["num1"].asInt();
    int num2 = params2["num2"].asInt();
    Json::Value result;
    result["result"] = num1 + num2;
    rpcRsp->setResult(result);
    rpcRsp->setRCode(RCode::RCODE_OK);
    std::string resultStr = rpcRsp->serialize();
    std::cout << "resultStr: " << resultStr << std::endl;

    // 3.消息的反序列化
    BaseMessage::BaseMessagePtr msgTmp = MessageFactory::create(MType::REQ_RPC);
    if (!msgTmp->unSerialize(msgStr))
    {
        LOG_ERROR("unserialize error");
        return 0;
    }
    std::cout << "msgTmpStr: " << msgTmp->serialize() << std::endl;
    return 0;
}